//登录和注册
const express = require("express");
const router = express.Router();
const Profile = require("../../model/profile");
const passport = require("passport");
const profile = require("../../model/profile");

//get请求 api/profile/test
router.get("/test", (req, res) => {
    res.json({ msg: "profile haha" });
});

//get请求 api/profile
//获取所有信息
router.get(
    "/",
    passport.authenticate("jwt", { session: false }),
    (req, res) => {
        Profile.find()
            .then((profile) => {
                if (!profile) {
                    return res.status(404).json("没有任何内容");
                }
                res.json(profile);
            })
            .catch((err) => res.status(404).json(err));
    }
);
//get请求 api/profile/:id
//获取单个信息
router.get(
    "/:id",
    passport.authenticate("jwt", { session: false }),
    (req, res) => {
        Profile.findOne({ _id: req.params.id })
            .then((profile) => {
                if (!profile) {
                    return res.status(404).json("没有任何内容");
                }
                res.json(profile);
            })
            .catch((err) => res.status(404).json(err));
    }
);
//post请求 api/profile/add
//创建信息接口
router.post(
    "/add",
    passport.authenticate("jwt", { session: false }),
    (req, res) => {
        const profileFields = {};
        if (req.body.type) profileFields.type = req.body.type;
        if (req.body.describe) profileFields.describe = req.body.describe;
        if (req.body.income) profileFields.income = req.body.income;
        if (req.body.cash) profileFields.cash = req.body.cash;
        if (req.body.expend) profileFields.expend = req.body.expend;
        if (req.body.remark) profileFields.remark = req.body.remark;
        new Profile(profileFields).save().then((profile) => {
            res.json(profile);
        });
    }
);
//post请求 api/profile/edit
//编辑信息接口
router.post(
    "/edit/:id",
    passport.authenticate("jwt", { session: false }),
    (req, res) => {
        const profileFields = {};
        if (req.body.type) profileFields.type = req.body.type;
        if (req.body.describe) profileFields.describe = req.body.describe;
        if (req.body.income) profileFields.income = req.body.income;
        if (req.body.cash) profileFields.cash = req.body.cash;
        if (req.body.expend) profileFields.expend = req.body.expend;
        if (req.body.remark) profileFields.remark = req.body.remark;
        Profile.findOneAndUpdate({ _id: req.params.id }, { $set: profileFields }, { new: true }).then((profile) => res.json(profile));
    }
);

//delete请求 api/profile/delete/:id
//删除信息接口
router.delete(
    "/delete/:id",
    passport.authenticate("jwt", { session: false }),
    (req, res) => {
        Profile.findOneAndRemove({ _id: req.params.id })
            .then((profile) => {
                profile.isNew = true;
                res.json(profile);
            })
            .catch((err) => res.status(404).json("删除失败!"));
    }
);

module.exports = router;